package com.ruoyi.common.flowable;


import com.ruoyi.common.utils.spring.SpringUtils;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * flowable 全局监听器配置类
 * @author: Lu Yang
 */
@Configuration
public class FlowableListenerConfig {

    /**
     * 将自定义监听器纳入flowable监听
     * @author: Lu Yang
     * @date: 2019/5/4 21:05
     * @param
     * @return org.flowable.spring.boot.EngineConfigurationConfigurer<org.flowable.spring.SpringProcessEngineConfiguration>
     */
    @Bean
    public EngineConfigurationConfigurer<SpringProcessEngineConfiguration> globalListenerConfigurer () {
        return engineConfiguration -> {
            engineConfiguration.setTypedEventListeners(this.customFlowableListeners());
        };
    }

    /**
     * 监听类配置 {@link FlowableEngineEventType} flowable监听器级别
     * @author: Lu Yang
     * @date: 2019/5/4 20:58
     * @param
     * @return java.util.Map<java.lang.String,java.util.List<org.flowable.common.engine.api.delegate.event.FlowableEventListener>>
     */
    private Map<String, List<FlowableEventListener>> customFlowableListeners () {
        Map<String, List<FlowableEventListener>> listenerMap = new HashMap<>();
        listenerMap.put(FlowableEngineEventType.PROCESS_CREATED.name(), getProcessCreatedListener());
        listenerMap.put(FlowableEngineEventType.PROCESS_STARTED.name(), getProcessStartedListener());
        listenerMap.put(FlowableEngineEventType.PROCESS_COMPLETED.name(), getProcessCompletedListener());
        listenerMap.put(FlowableEngineEventType.PROCESS_CANCELLED.name(), getProcessCancelledListener());


        Enum<?>[] enums = FlowableEngineEventType.class.getEnumConstants();
        Enum[] var4 = enums;
        int var5 = enums.length;
        for(int var6 = 0; var6 < var5; ++var6) {
            Enum<?> e = var4[var6];
            FlowableEngineEventType type = (FlowableEngineEventType) e;
            if(type.name().startsWith("ENTITY_")){
                continue;
            }
            put(listenerMap, (FlowableEngineEventType) e, SpringUtils.getBean(ProcessFullListener.class));
        }
        return listenerMap;
    }

    private void put(Map<String, List<FlowableEventListener>> listenerMap ,FlowableEngineEventType type , Object c){
        if(!listenerMap.containsKey(type.name())){
            listenerMap.put(type.name(),new ArrayList<>());
        }
        listenerMap.get(type.name()).add((FlowableEventListener) c);
    }

    public List<FlowableEventListener> getTaskCompletedListener() {
        List<FlowableEventListener> list = new ArrayList<>();
        list.add(SpringUtils.getBean(TaskCompletedListener.class));
        return list;
    }

    public List<FlowableEventListener> getTaskCreatedListener() {
        List<FlowableEventListener> list = new ArrayList<>();
        list.add(SpringUtils.getBean(TaskCreatedListener.class));
        return list;
    }

    public List<FlowableEventListener> getProcessCancelledListener() {
        List<FlowableEventListener> list = new ArrayList<>();
        list.add(SpringUtils.getBean(ProcessCancelledListener.class));
        return list;
    }

    public List<FlowableEventListener> getProcessCompletedListener() {
        List<FlowableEventListener> list = new ArrayList<>();
        list.add(SpringUtils.getBean(ProcessCompletedListener.class));
        return list;
    }

    public List<FlowableEventListener> getProcessCreatedListener() {
        List<FlowableEventListener> list = new ArrayList<>();
        list.add(SpringUtils.getBean(ProcessCreatedListener.class));
        return list;
    }

    public List<FlowableEventListener> getProcessStartedListener() {
        List<FlowableEventListener> list = new ArrayList<>();
        list.add(SpringUtils.getBean(ProcessStartedListener.class));
        return list;
    }
}
